package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymatou.iapi.thirdpartysync.parameter.getProducttListBean;
import com.ymatou.iapi.thirdpartysync.service.getProducttListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
/**
*查询分期乐商品信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProducttList{
	private static getProducttListBean bean;
	private static getProducttListCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询分期乐商品信息");
	}

	@Before
	public void caseUp() {
		bean=new getProducttListBean();
		call=new getProducttListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private String AddProduct() throws Exception {
		fenqileproductExample productExample=new fenqileproductExample();
		thirdpartysyncWapper syncWapper=new thirdpartysyncWapper(); 
		fenqilesku sku=new fenqilesku();
		fenqileskuExample skuExample=new fenqileskuExample();
		
		addProductBean bean=ThirdpartysyncCallService.initAddProductBean();
		String productId=bean.getProductIdList().get(0);
		
		ThirdpartysyncCallService.AddProductCall(bean,productId);
		
		productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
		
		skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
		
		int times=0;
		do{
		 	 if(prodcuts_DB.size()==0 )
			 {
		 	    times+=2000;
				Thread.sleep(times);
				prodcuts_DB=syncWapper.selectByExample(productExample);
				skus_DB=syncWapper.selectByExample(skuExample);
				Logger.comment("查询数据库休眠的时间："+times);
				if(prodcuts_DB.size()==0) continue;
			 }else	 break;
	    }while(times<=10000);

		if(times>10000){
			Logger.comment("商品同步到分期乐异常");
		    return null;
		}
		else {
			 return productId;
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProducttList_001() {
		Logger.start(true,"正例-查询分期乐商品信息-未同步");
		try {
			List<String> ids=new  ArrayList();
			ids.add("p2659204");
			bean.setProductIdList(ids);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");

			JSONArray array=new JSONObject(call.getData()).getJSONArray("productList");
			Logger.verifyEquals(bean.getProductIdList().get(0),new JSONObject(array.get(0).toString()).get("productId"), "验证productId");
			Logger.verifyEquals(0,new JSONObject(array.get(0).toString()).get("syncStatus"), "验证syncStatus");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProducttList_001_1() {
		Logger.start(true,"正例-查询分期乐商品信息-已同步");
		try {
			String productId=AddProduct();
			List<String> ids=new  ArrayList();
			ids.add(productId);
			bean.setProductIdList(ids);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");

			JSONArray array=new JSONObject(call.getData()).getJSONArray("productList");
			Logger.verifyEquals(bean.getProductIdList().get(0),new JSONObject(array.get(0).toString()).get("productId"), "验证productId");
			Logger.verifyEquals(1,new JSONObject(array.get(0).toString()).get("syncStatus"), "验证syncStatus");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProducttList_002() {
		Logger.start(true,"反例-查询分期乐商品信息-商品id中含有不存在的商品id");
		try {
			List<String> ids=new  ArrayList();
			ids.add("p2659204");
			ids.add("1111");
			bean.setProductIdList(ids);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");

			JSONArray array=new JSONObject(call.getData()).getJSONArray("productList");
			Logger.verifyEquals(1, array.length(), "验证返回的长度为1");
			Logger.verifyEquals(bean.getProductIdList().get(0),new JSONObject(array.get(0).toString()).get("productId"), "验证productId");
			Logger.verifyEquals(0,new JSONObject(array.get(0).toString()).get("syncStatus"), "验证syncStatus");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProducttList_003() {
		Logger.start(true,"反例-查询分期乐商品信息-商品id为空");
		try {
			
			List<String> ids=new  ArrayList();
			bean.setProductIdList(ids);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("false", call.getSuccess(), "验证success返回false");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProducttList_004() {
		Logger.start(true,"反例-查询分期乐商品信息-商品id不存在");
		try {
			List<String> ids=new  ArrayList();
			ids.add("111");
			bean.setProductIdList(ids);
			call.setData(bean);
			call.callService();		
			JSONArray array=new JSONObject(call.getData()).getJSONArray("productList");
			Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");
			Logger.verifyEquals(0, array.length(), "验证商品id不存在时返回长度为0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}